package comm.parsers;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import comm.util.SoptHashMap;

public class YahooSAXParserHandler extends DefaultHandler {
	private StringBuffer num = new StringBuffer();
	private StringBuffer lat = new StringBuffer();
	private StringBuffer lon = new StringBuffer();
	private StringBuffer name = new StringBuffer();
	private StringBuffer street = new StringBuffer();
	private StringBuffer city = new StringBuffer();
	private StringBuffer county = new StringBuffer();
	private StringBuffer state = new StringBuffer();
	private StringBuffer country = new StringBuffer();
	  
	  private boolean isNum = false;
	  private boolean isLat = false;
	  private boolean isLon = false;
	  private boolean isName = false;
	  private boolean isStreet = false;
	  private boolean isCity = false;
	  private boolean isCounty = false;
	  private boolean isState = false;
	  private boolean isCountry = false;
	  
	  private boolean isTotalcount = false;
	 
	  private String currentBuffer = "";
	  private String totalcount = "";
	  
	  public int itemNum=1;
	  
	  
	  public ArrayList itemList = new ArrayList();
	  
	 
	  public ArrayList getItemList(){
		  return itemList;
	  }
	  
	  @Override
	
	  
	  public void startElement(String uri, String localName,
	                     String qName, Attributes atts)
	  {
		  
	      if (qName.equals("num")) {
	    	  isNum=true;  num.setLength(0);
	      }else if (qName.equals("latitude")) {
	    	  isLat=true;  lat.setLength(0); 
	      }else if (qName.equals("longitude")) {
	    	  isLon=true;  lon.setLength(0); 
	      }else if (qName.equals("name")) {
	    	  isName=true;  name.setLength(0); 
	      }else if (qName.equals("street")) {
	    	  isStreet=true;  street.setLength(0); 
	      }else if (qName.equals("city")) {
	    	  isCity=true;  city.setLength(0); 
	      }else if (qName.equals("county")) {
	    	  isCounty=true;  county.setLength(0); 
	      }else if( qName.equals("state")){
	    	  isState=true; state.setLength(0);
	      }else if( qName.equals("country")){
	    	  isCountry=true; country.setLength(0);
	      }else if( qName.equals("Found")){
	    	  isTotalcount=true;
	      }
	  }	  
	  
	  public void endElement(String uri, String localName,
	                     String qName)
	 { 
		  currentBuffer = "";
		   
		  if (qName.equals("item")){
	          //저장!~
			  SoptHashMap tempHm = new SoptHashMap();
			  
			  tempHm.put("totalcount",totalcount);
			  tempHm.put("num", num.toString());
			  tempHm.put("name", name.toString());
			  tempHm.put("lat", lat.toString());
			  tempHm.put("lon", lon.toString());
			  tempHm.put("name",name.toString());
			  tempHm.put("street",street.toString());
			  tempHm.put("city",city.toString());
			  tempHm.put("county",county.toString());
			  tempHm.put("state",state.toString());
			  tempHm.put("country",country.toString());
			  
			  itemList.add(tempHm);
			  
	          itemNum++;
	      }else if (isNum) {
	    	  isNum=false; 
	      }else if (isName) {
	    	  isName=false;
	      }else if (isLat) {
	    	  isName=false;
	      }else if (isLon) {
	    	  isLon=false;
	      }else if (isName) {
	    	  isName=false;
	      }else if (isStreet) {
	    	  isStreet=false;
	      }else if (isCity) {
	    	  isCity=false; 
	      }else if( isCounty){
	    	  isCounty=false;
	      }else if( isState ){
	    	  isState=false; 
	      }else if( isCountry){
	    	  isCountry=false; 
	      }else if(isTotalcount){
	    	  isTotalcount=false;
	      }
	  }
	  public void characters(char[] chars, int start, int leng) 
	  { 
		  String temp = new String(chars, start, leng);
		  
	      if (isNum) {
	    	  isNum=false; num.append(chars,start,leng);
	      }else if (isName) {
	    	  isName=false;  name.append(chars,start,leng);
	      }else if (isLat) {
	    	  isLat=false;  lat.append(chars,start,leng);
	      }else if (isLon) {
	    	  isLon=false;  lon.append(chars,start,leng);
	      }else if (isName) {
	    	  isName=false;  name.append(chars,start,leng);
	      }else if (isStreet) {
	    	  isStreet=false;  street.append(chars,start,leng);
	      }else if (isCity) {
	    	  isCity=false; city.append(chars,start,leng);
	      }else if (isCounty) {
	    	  isCounty=false;  county.append(chars,start,leng);
	      }else if( isState ){
	    	  isState=false; state.append(chars,start,leng);
	      }else if( isCountry){
	    	  isCountry=false; country.append(chars,start,leng);
	      }else if(isTotalcount){
	    	  isTotalcount=false; totalcount=temp;
	      }else{ //<b>태그 문제 해결 부분- currentBuffer를 통해 < 태그 이후에 글씨들을 받아서 append
	    	  
	    	  if(currentBuffer.equals("title"))
	    		 System.out.println("");
	    	  else if(currentBuffer.equals("description"))
	    		  System.out.println("");
	      }
	  }
}
